package net.sxjr.common;

import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * @author ：cuixc
 * @date ：Created in 2020/12/10 15:25
 * @description：md5加密
 * @modified By：
 */
public class MD5 {
    public static String encodeByMd5(String string){
        try {


        // 确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        Base64.Encoder base64Encoder = Base64.getEncoder();
        // 加密字符串
            return base64Encoder.encodeToString(md5.digest(string.getBytes("utf-8")));
        }catch (Exception e){
            return null;
        }

    }

    public static String Md5(String string){
        return DigestUtils.md5DigestAsHex(string.getBytes(StandardCharsets.UTF_8));
    }
}
